const request = require('request');
const mongoose = require('mongoose');
const async = require('async');

const gzf = require('./gzfSchema').gzf;

const url = 'http://bzflh.szjs.gov.cn/lhmcAction.do?method=queryYgbLhmcList';

let form = {
    pageNumber: 1,
    pageSize: 1000,
    waittype: 2,
    num: 0,
    shoulbahzh: '',
    xingm: '',
    idcard: '',
};

let page = 1, totalPage;

function record(page) {
    form.pageNumber = page;
    console.log('正在请求第%s页数据...', page);
    request({
        url,
        method: "POST",
        json: true,
        form
    },
        function (error, response, body) {
            if (error) {
                return console.log(error);
            }
            else if (!error && response.statusCode == 200) {
                console.log('获取到了第%s页数据， 共有%s条。', page, body.rows.length);
                totalPage = Math.ceil(body.total / form.pageSize);
                let rows = body.rows;
                async.eachLimit(rows, 50, function (row, cb) {
                    gzf.find({ SHOULHZH: row.SHOULHZH }, function (err, doc) {
                        if (err) {
                            return cb(err);
                        }
                        else {
                            if (doc.length > 0) {
                                row.UPDATE_TIME = new Date().toLocaleString();
                                gzf.update({ SHOULHZH: row.SHOULHZH }, row, { multi: true }, function (err, raw) {
                                    if (err) {
                                        cb(err);
                                    }
                                    cb(null);
                                });
                            }
                            else {
                                row.TIME = new Date().toLocaleString();
                                gzf.create(row, function (err, res) {
                                    if (err) {
                                        return cb(err);
                                    }
                                    else {
                                        cb(null);
                                    }
                                })
                            }
                        }
                    })
                }, function (err, rel) {
                    if (err) {
                        console.log(err);
                        process.exit();
                        return;
                    }
                    page++;
                    if (page <= totalPage) {
                        record(page);
                    }
                    else {
                        console.log('已经同步完数据。');
                        process.exit();
                    }
                });
            }
            else {
                console.log('查询失败：%s', response.statusCode);
                return;
            }
        });
}

record(1);